<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
	<title>New document</title>
	<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<script type="text/javascript">
	function Timer(tick, callback) {
		this.id = null;
		this.tick = tick;
		this.callback = callback;
		this.status = Timer.TimerStatus.CLEAR;
		this.args = null;
		this.timeElapsed = 0;
		this.maxTime = 0;
	}

	Timer.TimerStatus = {
		CLEAR: 0,
		STARTED: 1,
		STOPPED: 2
	};

	Timer.prototype.start = function() {
		var args = this.args = arguments;
		var self = this;
		if (this.status == Timer.TimerStatus.CLEAR || this.status == Timer.TimerStatus.STOPPED) {
			var cb = function() {
				if (typeof(self.callback) == "function")
					self.callback(args);
				if (self.maxTime > 0)
					self.timeElapsed += self.tick;
				if (self.maxTime > 0 && self.timeElapsed > self.maxTime)
					self.stop();
			};
			this.id = setInterval(cb, this.tick);
			this.status = Timer.TimerStatus.STARTED;
		}
	}

	Timer.prototype.stop = function() {
		if (this.status == Timer.TimerStatus.STARTED) {
			window.clearInterval(this.id);
			this.status = Timer.TimerStatus.STOPPED;
			this.timeElapsed = 0;
		}
	}

	Timer.prototype.setTick = function(tick) {
		this.tick = tick;
		if (this.status == Timer.TimerStatus.STARTED) {
			this.stop();
			this.start(this.args);
		}
	}

	var cb = function cab(cb) {
		alert("I'm here to go-" + this.timeElapsed);
	}

	//var timer = new Timer(5000, cb);
	//timer.start(8);
</script>
</body>
</html>